package com.chengh.vhr.controller.emp;

import com.chengh.vhr.common.utils.POIUtils;
import com.chengh.vhr.model.*;
import com.chengh.vhr.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 员工基本资料接口
 * @author chengh
 */
@RestController
@RequestMapping("/emp/basic")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @Autowired
    NationService nationService;

    @Autowired
    PoliticsStatusService politicsStatusService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    JobLevelService jobLevelService;

    @Autowired
    PositionService positionService;

    /**
     * 分页获得员工的基本资料
     * @param pageRequest 请求分页类,
     * @param keyword 关键字
     * @return
     */
    @GetMapping("/")
    public PageInfo<Employee> getPageEmployee(PageRequest pageRequest,String keyword) {
        return employeeService.getPageEmployee(pageRequest,keyword);
    }

    /**
     * 添加员工
     * @param employee 员工实体类
     * @return
     */
    @PostMapping("/")
    public RespBean addEmployee(@RequestBody Employee employee) {
        if(employeeService.addEmployee(employee) == 1) {
            return RespBean.ok("添加成功!");
        }
        return RespBean.error("添加失败!");
    }

    /**
     * 获得所有的名族
     * @Author chengh
     * @return
     */
    @GetMapping("/getNations")
    public List<Nation> getNations() {
        return nationService.getNations();
    }

    /***
     * @Description 获得所有的政治面貌列表
     * @return java.util.List<com.chengh.vhr.model.PoliticsStatus>
     * @Author chengh
     * @Datetime 2021/1/25 20:09
     */
    @GetMapping("/getPoliticsStatus")
    public List<PoliticsStatus> getPoliticsStatus() {
        return politicsStatusService.getPoliticsStatus();
    }

    /**
     * @Description 获得所有的部门信息列表
     * @return java.util.List<com.chengh.vhr.model.Department> 
     * @Author chengh
     * @Datetime 2021/1/25 20:11
     */
    @GetMapping("/getDepartments")
    public List<Department> getDepartments() {
        return departmentService.getAllDepartments();
    }

    /**
     * @Description 获得所有的职称列表
     * @return java.util.List<com.chengh.vhr.model.JobLevel>
     * @Author chengh
     * @Datetime 2021/1/25 20:13
     */
    @GetMapping("/getJobLevel")
    public List<JobLevel> getJobLevel() {
        return jobLevelService.getAllJobLevel();
    }

    /**
     * @Description 获得所有的职位列表
     * @return java.util.List<com.chengh.vhr.model.Position>
     * @Author chengh
     * @Datetime 2021/1/25 20:14
     */
    @GetMapping("/getPositions")
    public List<Position> getPositions() {
        return positionService.getAllPosition();
    }

    /***
     * @Description  新增员工时获得指定的工号
     * @return  com.chengh.vhr.model.RespBean
     * @Author  chengh
     * @Datetime  2021/1/28 20:56
     */
    @GetMapping("getMaxWorkID")
    public RespBean getMaxWorkID() {
        return RespBean.ok("",String.format("%08d",employeeService.getMaxWordID()));
    }

    /**
     * 根据员工id删除员工信息
     * @Author chengh
     * @param ids id结合
     * @Date 2021/1/31 19:56
     * @Return com.chengh.vhr.model.RespBean
     */
    @DeleteMapping("/")
    public RespBean deleteEmps(@RequestBody List<Integer> ids) {
        if(ids.size() == employeeService.deleteEmps(ids)){
            return RespBean.ok("删除成功员工成功!");
        }
        return RespBean.error("删除员工失败!");
    }

    /**
     * 更新员工资料信息
     * @Author chengh
     * @param employee
     * @Date 2021/1/31 19:56
     * @Return com.chengh.vhr.model.RespBean
     */
    @PutMapping("/")
    public RespBean updateEmp(@RequestBody Employee employee) {
        if(1 == employeeService.updateEmp(employee)) {
            return RespBean.ok("更新员工信息成功!");
        }
        return RespBean.error("更新员工信息失败!");
    }

    /**
     * 下载文件
     * @Author chengh
     * @param
     * @Date 2021/1/31 20:45
     * @Return org.springframework.http.ResponseEntity<byte[]>
     */
    @GetMapping("/exportData")
    public ResponseEntity<byte[]> exportData() {
        List<Employee> list = employeeService.getPageEmployee(new PageRequest(1, 1000), null).getList();
        return POIUtils.employee2Excel(list);
    }

    @PostMapping("/importData")
    public RespBean impprotData(MultipartFile file) throws IOException {
        file.transferTo(new File("C:\\Users\\chengh\\Desktop\\上传文件.xls"));
        return RespBean.ok("上传成功!");
    }
}
